package com.ruoyi.production.controller;

import com.ruoyi.base.domain.TechnologicalProcess;
import com.ruoyi.base.service.ITechnologicalProcessService;
import com.ruoyi.base.vo.TechnologicalProcessViewVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.production.domain.PlanModelInfo;
import com.ruoyi.production.domain.WorkOrderInfo;
import com.ruoyi.production.service.IWorkOrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工单管理Controller
 *
 * @author ruoyi
 * @date 2022-09-01
 */
@RestController
@RequestMapping("/production/WorkOrderInfo")
public class WorkOrderInfoController extends BaseController {
    @Autowired
    private IWorkOrderInfoService workOrderInfoService;

    @Autowired
    private ITechnologicalProcessService technologicalProcessService;


    /**
     * 查询工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderInfo workOrderInfo) {
        startPage();
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrderInfo workOrderInfo) {
        List<WorkOrderInfo> list = workOrderInfoService.selectWorkOrderInfoList(workOrderInfo);
        ExcelUtil<WorkOrderInfo> util = new ExcelUtil<WorkOrderInfo>(WorkOrderInfo.class);
        util.exportExcel(response, list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(workOrderInfoService.selectWorkOrderInfoById(id));
    }

    /**
     * 新增工单管理
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrderInfo workOrderInfo) {
        return toAjax(workOrderInfoService.insertWorkOrderInfo(workOrderInfo));
    }

    /**
     * 修改工单管理
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:edit')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrderInfo workOrderInfo) {
        check(workOrderInfo.getId());
        return toAjax(workOrderInfoService.updateWorkOrderInfo(workOrderInfo));
    }

    /**
     * 构件列表
     */
    @GetMapping("/getCompomentList")
    public AjaxResult getCompomentList(@RequestBody PlanModelInfo planModelInfo) {
        if (StringUtils.isBlank(planModelInfo.getProjectNo())){
            AjaxResult.error("项目编号不能为空!");
        }
        List<PlanModelInfo> compomentList = workOrderInfoService.getCompomentList(planModelInfo);
        return AjaxResult.success(compomentList);
    }

    /**
     * 删除工单管理
     */
    @PreAuthorize("@ss.hasPermi('production:WorkOrderInfo:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            check(id);
        }
        return toAjax(workOrderInfoService.deleteWorkOrderInfoByIds(ids));
    }


    /**
     * 获取工艺流程
     *
     * @param technologicalProcess
     * @return
     */
    @GetMapping("/getTechnologicalProcesslist")
    public TableDataInfo list(TechnologicalProcess technologicalProcess) {
        List<TechnologicalProcess> list = technologicalProcessService.selectTechnologicalProcessList(technologicalProcess);
        return getDataTable(list);
    }

    /**
     * 获取工艺流程详细信息
     */
    @GetMapping(value = "/getTechnologicalProcess/{id}")
    public AjaxResult getTechnologicalProcess(@PathVariable("id") Long id) {
        TechnologicalProcessViewVo viewVo = technologicalProcessService.selectTechnologicalProcessById(id);
        return AjaxResult.success(viewVo.getRel());
    }

    //删除工单内构件


    public void check(Long id) {
        WorkOrderInfo workOrderInfo = workOrderInfoService.selectWorkOrderInfoById(id);
        if (!"0".equals(workOrderInfo.getStatus())) {
            throw new ServiceException("请确认工单状态，删除失败！");
        }
    }

}
